#include #include // Define the pins connected to the servo and LEDs int servoPin = 44; int ledPin = 7; int green = 8; int red = 9; Servo myServo; // Define a code, later on this will be sent to the box from the app String correctCode = "12345"; // Change this to your desired code String inputCode = ""; // Variable to store input from the keypad // Setup keypad pins and layout const byte ROWS = 4; // 4 rows const byte COLS = 3; // 3 columns char keys[ROWS][COLS] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte rowPins[ROWS] = {43, 6, 5, 4}; byte colPins[COLS] = {3, 2, 1}; Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); void setup() { // Attach the servo on the specified pin to the servo object myServo.attach(servoPin); myServo.write(0); // Initialize the servo to 0 degrees (closed) // Set LED pins as outputs pinMode(ledPin, OUTPUT); pinMode(green, OUTPUT); pinMode(red, OUTPUT); // Ensure LEDs are off initially digitalWrite(ledPin, LOW); digitalWrite(green, LOW); digitalWrite(red, LOW); // Begin Serial communication for debugging Serial.begin(115200); } void loop() { // Check for code, if code input by user == code defined - move servo char key = keypad.getKey(); // Get the pressed key if (key) { // If a key is pressed if (key == '#') { // '#' will be used as the "Enter" key if (inputCode == correctCode) { // Correct code entered, move the servo and turn on green LED Serial.println("Correct Code! Opening box..."); digitalWrite(green, HIGH); // Turn on the green LED openBox(); // Call the function to open the box (servo movement) delay(1000); // Keep the green LED on for 1 second digitalWrite(green, LOW); // Turn off the green LED } else { // Incorrect code, reset input and turn on red LED Serial.println("Incorrect Code. Try again."); digitalWrite(red, HIGH); // Turn on the red LED delay(1000); // Keep the red LED on for 1 second digitalWrite(red, LOW); // Turn off the red LED inputCode = ""; // Clear the input } } else if (key == '*') { // '*' will be used to clear the input Serial.println("Clearing input..."); inputCode = ""; // Reset the inputCode } else { // Append pressed key to inputCode inputCode += key; Serial.println("Entered: " + inputCode); } } } // Function to move the servo and simulate opening the box void openBox() { myServo.write(180); // Move the servo to 180 degrees (open) digitalWrite(ledPin, HIGH); // Turn on the LED (indicating the door is open) delay(2000); // Keep the box open for 2 seconds myServo.write(0); // Move the servo back to 0 degrees (closed) digitalWrite(ledPin, LOW); // Turn off the LED (indicating the door is closed) }